package com.hengda.museumonline.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.BaseAdapter;
import com.hengda.museumonline.adapter.BaseLoadMoreHeaderAdapter;
import com.hengda.museumonline.adapter.BaseViewHolder;
import com.hengda.museumonline.adapter.ErrorAdapter;
import com.hengda.museumonline.entity.CommentBean;
import com.hengda.museumonline.entity.EngineeringBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.http.GetData;
import com.hengda.museumonline.view.EngineeringRecyclerView;
import com.hengda.museumonline.view.InputContentPopupWindon;
import com.hengda.museumonline.view.KeyboardDetectorRelativeLayout;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.List;

/**
 * @author：JFWU on 2016/10/21 10:53
 * @E-mail：jeffy12138@126.com
 */
public class NewsCommentActivity extends Activity {
    private InputContentPopupWindon popu;
    private android.widget.LinearLayout ll;
    private KeyboardDetectorRelativeLayout rootlayout;
    private int id;
    private android.support.v7.widget.RecyclerView recyclerview;
    private android.widget.ImageView loadingbg;
    private CommentBean commentBean1 = new CommentBean();
    private CommentBean commentBean2 = new CommentBean();
    private MyAdapter mAdapter;
    private int i = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newscomment);
        this.loadingbg = (ImageView) findViewById(R.id.loading_bg);
        this.recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(recyclerview.getContext()));
        this.rootlayout = (KeyboardDetectorRelativeLayout) findViewById(R.id.root_layout);
        this.ll = (LinearLayout) findViewById(R.id.ll);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ll.setVisibility(View.INVISIBLE);
                InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                manager.showSoftInput(view, InputMethodManager.RESULT_SHOWN);
                manager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });
        id = getIntent().getIntExtra("id", -1);
        rootlayout.setOnSoftKeyboardListener(new KeyboardDetectorRelativeLayout.OnSoftKeyboardListener() {
            @Override
            public void onShown(int keyboardHeight) {
                showPopupwin();
            }

            @Override
            public void onMeasureFinished() {
            }

            @Override
            public void onHidden() {
                if (popu != null) {
                    popu.dismiss();
                    ll.setVisibility(View.VISIBLE);
                }
            }
        });
        getCommentfromNet();
    }

    private void getCommentfromNet() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "comment/index", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("offset", 0);
        request.add("pernum", 20);
        request.add("type", 0);
        request.add("id", id);
        CallServer.getRequestInstance().add(this, 0, request, listener);
    }

    OnResponseListener listener = new OnResponseListener() {
        @Override
        public void onStart(int what) {
        }

        @Override
        public void onSucceed(int what, Response response) {
            System.out.println("评论结果:" + response.get().toString());
            if (200 == response.getHeaders().getResponseCode()) {
                Gson gson = new Gson();
                if (0 == what) {
                    System.out.println("结果：" + response.get().toString());
                    commentBean1 = gson.fromJson(response.get().toString(), CommentBean.class);
                    if (commentBean1 != null && commentBean1.getData() != null && commentBean1.getData().size() > 0) {
                        loadingbg.setVisibility(View.INVISIBLE);
                        mAdapter = new MyAdapter(NewsCommentActivity.this, recyclerview, commentBean1.getData(), R.layout.newscomment_item);
                        recyclerview.setAdapter(mAdapter);
                        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
                            @Override
                            public void onLoadMore() {
                                Request request = NoHttp.createStringRequest(MyApplication.url + "comment/index", RequestMethod.GET);
                                request.add("p", "a");
                                request.add("v", PackageUtil.getAppPackageInfo(NewsCommentActivity.this).versionName);
                                request.add("offset", 0);
                                request.add("pernum", 20);
                                request.add("type", 20 * i);
                                request.add("id", id);
                                i += 1;
                                CallServer.getRequestInstance().add(NewsCommentActivity.this, 1, request, listener);
                            }
                        });
                    } else {
                        recyclerview.setAdapter(new ErrorAdapter(NewsCommentActivity.this));
                    }
                } else if (1 == what) {
                    commentBean2 = gson.fromJson(response.get().toString(), CommentBean.class);
                    if (commentBean2.getData() == null) {
                        mAdapter.setLoading(false);
                    } else {
                        mAdapter.addAll(commentBean2.getData());
                    }
                }
            }
        }

        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
            System.out.println("onFailed:" + exception);
            recyclerview.setAdapter(new ErrorAdapter(NewsCommentActivity.this));
        }

        @Override
        public void onFinish(int what) {
        }
    };

    class MyAdapter extends BaseLoadMoreHeaderAdapter<CommentBean.DataBean> {

        public MyAdapter(Context mContext, RecyclerView recyclerView, List<CommentBean.DataBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(final Context mContext, RecyclerView.ViewHolder holder, final CommentBean.DataBean dataBean) {
            ((BaseViewHolder) holder).setText(R.id.title, dataBean.getTitle());
            ((BaseViewHolder) holder).setText(R.id.content, dataBean.getContent());
        }
    }

    public void showPopupwin() {
        popu = new InputContentPopupWindon(this, onClicklistener);
        popu.setOutsideTouchable(false);//点击屏幕意外位置消失
        popu.setFocusable(true);
        SharedPreferences pref = this.getSharedPreferences("in_put", Context.MODE_PRIVATE);
        int size = pref.getInt("size", 0);
        popu.showAtLocation(this.findViewById(R.id.root_layout), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, size);

    }

    View.OnClickListener onClicklistener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.send_btn:
                    Toast.makeText(NewsCommentActivity.this, ((EditText) (popu.getContentView().findViewById(R.id.input_et))).getText().toString(), Toast.LENGTH_SHORT).show();
                    v.requestFocus();
                    InputMethodManager manager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                    manager.hideSoftInputFromWindow(v.getWindowToken(), 0); //隐藏
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        if (popu != null) {
            popu.dismiss();
            ll.setVisibility(View.VISIBLE);
        }
    }
}
